package com.neolix.watch.util;

import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public class CommonHelper {


    public static final String TAG = CommonHelper.class.getSimpleName();
    private static Dialog mDialog = null;

    public static void showRequestDialog(Context context, String content) {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
        mDialog = new DialogFactory().createRequestDialog(context, content);
        mDialog.show();
    }

    public static void hideRequestDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    public static String getContactNameByPhoneNumber(Context context, String address) {
        String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, // Which columns to return.
//                ContactsContract.CommonDataKinds.Phone.NUMBER + " = '"
//                        + address + "'", // WHERE clause.
                ContactsContract.CommonDataKinds.Phone.NUMBER + " like ?",
                new String[]{'%' + address + '%'}, // WHERE clause value substitution
                null  // Sort order.
        );

        if (cursor == null) {
            Log.d(TAG, "getPeople null");
            return null;
        }
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);

            // 取得联系人名字
            int nameFieldColumnIndex = cursor
                    .getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
            String name = cursor.getString(nameFieldColumnIndex);
            return name;
        }
        return null;
    }
}
